/// mouse_event: 0.2.1 #https://pub.dev/packages/mouse_event 鼠标事件
import 'package:flutter/material.dart';
import 'package:mouse_event/mouse_event.dart';

class MouseData {
  ///fixme 测试发现，除了滚轮向上向下滚动，mouseData有值，其他按键基本都是0

  //鼠标向上滚动
  static final int MOUSEWHEEL_UP = 7864320;

  //鼠标向下滚动
  static final int MOUSEWHEEL_DOWN = 4287102976;
}

class MouseEventManage {
  ///开始监听
  static startListening() {
    //加上这行，防止应用初始化未完成，导致监听异常
    ///fixme 亲测有效，能够监听左右键以及滚轮，全局监听，就算不在当前应用内，也能监听。
    WidgetsFlutterBinding.ensureInitialized();
    MouseEventPlugin.startListening((mouseEvent) {
      //mouseEvent.x;
      //mouseEvent.y;
      //WM_MOUSEMOVE	MouseEventMsg.WM_MOUSEWHEEL
      // print("${mouseEvent.mouseMsg.name}\t${MouseEventMsg.WM_MOUSEWHEEL.name}");
      if (mouseEvent.mouseMsg.name == MouseEventMsg.WM_MOUSEWHEEL.name) {
        //滚轮
        // print("${mouseEvent.mouseMsg.runtimeType}");//MouseEventMsg
        //mouseEvent.mouseData 7864320 向上滚动
        //mouseEvent.mouseData 4287102976 向下滚动
        //fixme 测试发现，除了滚轮向上向下滚动，mouseData有值，其他按键基本都是0
        if (mouseEvent.mouseData == MouseData.MOUSEWHEEL_UP) {
          //滚轮向上滚动
          // print("滚轮向上滚动");
        } else if (mouseEvent.mouseData == MouseData.MOUSEWHEEL_DOWN) {
          //滚轮向下滚动
          // print("滚轮向下滚动");
        }
        // print("${mouseEvent.mouseData}");
      } else if (mouseEvent.mouseMsg.name ==
          MouseEventMsg.WM_MBUTTONDOWN.name) {
        //鼠标滚轮中键按下；mouseEvent.mouseData 为0
        //print("${mouseEvent.mouseData}");//0
      } else if (mouseEvent.mouseMsg.name == MouseEventMsg.WM_MBUTTONUP.name) {
        //鼠标滚轮中键离开；mouseEvent.mouseData 为0
        //print("${mouseEvent.mouseData}");//0
      } else if (mouseEvent.mouseMsg.name ==
          MouseEventMsg.WM_LBUTTONDOWN.name) {
        //左键按下 mouseEvent.mouseData 为0
        // print("${mouseEvent.mouseData}");//0
      } else if (mouseEvent.mouseMsg.name == MouseEventMsg.WM_LBUTTONUP.name) {
        //左键离开  mouseEvent.mouseData 为0
        // print("${mouseEvent.mouseData}");//0
      } else if (mouseEvent.mouseMsg.name ==
          MouseEventMsg.WM_RBUTTONDOWN.name) {
        //右键按下  mouseEvent.mouseData 为0
        // print("${mouseEvent.mouseData}");//0
      } else if (mouseEvent.mouseMsg.name == MouseEventMsg.WM_RBUTTONUP.name) {
        //右键离开  mouseEvent.mouseData 为0
        // print("${mouseEvent.mouseData}");//0
      }
      // print(mouseEvent.toString());
    });
  }

  ///停止监听
  static cancelListening() {
    MouseEventPlugin.cancelListening();
  }
}
